﻿#include "currenttestwidget.h"

QCurrentTestWidget::QCurrentTestWidget(QWidget *parent) : QParamBasicWidget(parent)
{
    InitUI();

    QString sInfo;
    sInfo = sInfo + QString("当前KP:0.0\n");
    sInfo = sInfo + QString("当前KI:0.0\n");
    sInfo = sInfo + QString("当前KD:0.0\n");
    _pShowPIDLabel->setText(sInfo);

    //显示状态
    st_loopbackdata rcvdata;
    sInfo = getFrameStatus(rcvdata);
    _pShowStatusLabel->setText(sInfo);


    connect(_pQueryParamBtn,&QPushButton::clicked,this,&QCurrentTestWidget::onQueryParamBtnClicked);
    connect(_pSetParamBtn,&QPushButton::clicked,this,&QCurrentTestWidget::onSetParamBtnClicked);


    _pPlotDataTimer = new QTimer(this);
    connect(_pPlotDataTimer,&QTimer::timeout,this,[=](){plotRcvData();});


    connect(_pStartBtn,&QPushButton::clicked,this,&QCurrentTestWidget::onStartBtnClicked);

    //默认选中方位轴
    _i32AixsNo = 0;
}

/**
* @brief
*
**/
void QCurrentTestWidget::InitUI()
{
    _pLogWidget = new QLogWidget(this);

    _pCurrentPlot = new QCustomPlot(this);
    _pCurrentABPlot = new QCustomPlot(this);

    setPlotStyle(_pCurrentPlot,"时间(ms)","电流值(A)");
    _pCurrentPlot->graph(0)->setName("Q电流设定值");
    _pCurrentPlot->graph(1)->setName("Q电流反馈值");

    setPlotStyle(_pCurrentABPlot,"时间(ms)","电流值(A)");
    _pCurrentABPlot->graph(0)->setName("A相电流");
    _pCurrentABPlot->graph(1)->setName("B相电流");

    _pParamWidget = createParamWidget();


    QGridLayout* playout = new QGridLayout(this);
    playout->setSpacing(0);

    playout->addWidget(_pCurrentABPlot,0,0,1,1);
    playout->addWidget(_pCurrentPlot,1,0,1,1);

    playout->addWidget(_pParamWidget,0,1,3,1);
    playout->addWidget(_pLogWidget,2,0,1,1);

    playout->setColumnStretch(0,3);
    playout->setColumnStretch(1,1);

    playout->setRowStretch(0,2);
    playout->setRowStretch(1,2);
    playout->setRowStretch(2,1);

}


/**
* @brief
*
**/
QWidget* QCurrentTestWidget::createPIDWidget()
{
    QWidget* pwidget = new QWidget(this);
//    QUIQss::setWidgetStyleSheet(pwidget);

    _pKPEdit = new QLineEdit("0",this);
    _pKIEdit = new QLineEdit("0",this);
    _pKDEdit = new QLineEdit("0",this);
    QUIQss::setTxtQss(_pKPEdit, "#DCE4EC", "#1ABC9C");
    QUIQss::setTxtQss(_pKIEdit, "#DCE4EC", "#1ABC9C");
    QUIQss::setTxtQss(_pKDEdit, "#DCE4EC", "#1ABC9C");


    _pQueryParamBtn = new QPushButton("查询参数",this);
    _pSetParamBtn = new QPushButton("设置参数",this);


    QLabel* plabel1 = new QLabel("控制器比例KP(V/A):",this);
    QLabel* plabel2 = new QLabel("控制器积分KI(Hz):",this);
    QLabel* plabel3 = new QLabel("控制器微分KD(Hz):",this);


    int nRow = 0;
    QGridLayout* playout = new QGridLayout(pwidget);


    playout->addWidget(plabel1,nRow,0,1,1);
    playout->addWidget(_pKPEdit,nRow,1,1,3);
    nRow++;

    playout->addWidget(plabel2,nRow,0,1,1);
    playout->addWidget(_pKIEdit,nRow,1,1,3);
    nRow++;
    playout->addWidget(plabel3,nRow,0,1,1);
    playout->addWidget(_pKDEdit,nRow,1,1,3);
    nRow++;
    playout->addWidget(_pQueryParamBtn,nRow,0,1,1);
    playout->addWidget(_pSetParamBtn,nRow,1,1,3);
    nRow++;

    playout->setColumnStretch(0,1);
    playout->setColumnStretch(1,1);
    playout->setColumnStretch(2,1);
    playout->setColumnStretch(3,1);

    return pwidget;
}

/**
* @brief
*
**/
QWidget* QCurrentTestWidget::createTestWidget()
{
    QWidget* pwidget = new QWidget(this);
    QUIQss::setWidgetStyleSheet(pwidget);

    _pSignalTypeComBox = new QComboBox(this);
    _pSignalTypeComBox->addItem("方波");
    _pSignalTypeComBox->addItem("正弦波");
    _pSignalTypeComBox->setMinimumHeight(30);

    _pFreqEdit = new QLineEdit("0",this);
    _pFreqEdit->setToolTip("频率范围:[0,20]Hz");

    _pAmpEdit = new QLineEdit("0",this);
    _pAmpEdit->setToolTip("幅值范围:[0,3]A");

    QUIQss::setTxtQss(_pFreqEdit, "#DCE4EC", "#1ABC9C");
    QUIQss::setTxtQss(_pAmpEdit, "#DCE4EC", "#1ABC9C");


    _pStartBtn = new QPushButton("开始测试",this);


    QLabel* plabel = new QLabel("信号类型:",this);
    QLabel* plabel3 = new QLabel("信号频率(Hz):",this);
    QLabel* plabel4 = new QLabel("幅值(A):",this);

    int nRow = 0;
    QGridLayout* playout = new QGridLayout(pwidget);

    playout->addWidget(plabel,nRow,0,1,1);
    playout->addWidget(_pSignalTypeComBox,nRow,1,1,3);
    nRow++;
    playout->addWidget(plabel3,nRow,0,1,1);
    playout->addWidget(_pFreqEdit,nRow,1,1,3);
    nRow++;
    playout->addWidget(plabel4,nRow,0,1,1);
    playout->addWidget(_pAmpEdit,nRow,1,1,3);
    nRow++;
    playout->addWidget(_pStartBtn,nRow,3,1,1);
    nRow++;

    return pwidget;

}

/**
* @brief
*
**/
QWidget* QCurrentTestWidget::createParamWidget()
{
    QWidget* pwidget = new QWidget(this);
    QUIQss::setWidgetStyleSheet(pwidget);

    _pShowPIDLabel = new QLabel(this);
    _pShowPIDLabel->setAlignment(Qt::AlignTop);
    _pShowPIDLabel->setStyleSheet("color:rgb(0,0,255);font-size:20px;font-family: 'Microsoft YaHei';");


    _pShowStatusLabel = new QLabel(this);
    _pShowStatusLabel->setAlignment(Qt::AlignTop);
    _pShowStatusLabel->setStyleSheet("color:rgb(0,0,255);font-size:20px;font-family: 'Microsoft YaHei';");

    QTabWidget* pTabwidget = new QTabWidget(this);

    pTabwidget->setTabPosition(QTabWidget::South);

    pTabwidget->addTab(createPIDWidget(),"控制参数");
    pTabwidget->addTab(createTestWidget(),"电流环测试");


    int nRow = 0;
    QGridLayout* playout = new QGridLayout(pwidget);

    playout->addWidget(pTabwidget,nRow,0,1,2);
    nRow++;
    playout->addWidget(_pShowPIDLabel,nRow,0,1,1);
    playout->addWidget(_pShowStatusLabel,nRow,1,1,1);

    playout->setRowStretch(0,1);
    playout->setRowStretch(1,2);

    return pwidget;
}

/**
* @brief
*
**/
void QCurrentTestWidget::setPlotStyle(QCustomPlot* pPlot,QString xName,QString yName)
{
    pPlot->xAxis->setLabel(xName);
    pPlot->yAxis->setLabel(yName);

//    pPlot->xAxis->setRange(0, 1024);//设置范围
//    pPlot->yAxis->setRange(0, 100);

    pPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom /*| QCP::iSelectPlottables */| QCP::iMultiSelect);

    pPlot->addGraph();//增加通道
    pPlot->addGraph();//增加通道

    pPlot->graph(0)->setName("设定值");
    pPlot->graph(1)->setName("反馈值");


    QPen pen;
    pen.setColor(QColor(0,0,255));
    pen.setWidth(1);
    pPlot->graph(0)->setPen(pen);


    pen.setColor(QColor(255,0,0));
    pPlot->graph(1)->setPen(pen);


    pPlot->legend->setVisible(true);
    pPlot->legend->setFont(QFont("Helvetica",9));

}

/**
* @brief
*
**/
void QCurrentTestWidget::onStartBtnClicked()
{
    if(_pStartBtn->text() == "开始测试")
    {
        _xdatalist.clear();
        _ydatalist[0].clear();
        _ydatalist[1].clear();
        _y1datalist[0].clear();
        _y1datalist[1].clear();
        _pCurrentPlot->graph(0)->data().data()->clear();//清除数据
        _pCurrentPlot->graph(1)->data().data()->clear();//清除数据
        _pCurrentABPlot->graph(0)->data().data()->clear();//清除数据
        _pCurrentABPlot->graph(1)->data().data()->clear();//清除数据
        _i32FrameCnt = 0;

        int l_i32SigalType = _pSignalTypeComBox->currentIndex()+1;//信号类型
        int l_i32Freq = _pFreqEdit->text().toFloat()*10;//频率
        short l_i16Amp = _pAmpEdit->text().toFloat()*10.0;//幅值

        fourchar l_data;
        l_data.i32data = 0x0;
        l_data.data[3] = (l_i32SigalType&0xff);
        l_data.data[2] = 0;
        l_data.data[1] = (l_i16Amp>>8);
        l_data.data[0] = (l_i16Amp&0xff);


        emit signal_startTest(_i32AixsNo,0x21,l_data.ui32data,l_i32Freq);


        _bStartTest = true;

        _pStartBtn->setText("停止测试");

        _pPlotDataTimer->start(500);

        _pLogWidget->onShowlog("开始测试..");
    }
    else if(_pStartBtn->text() == "停止测试")
    {
        _pPlotDataTimer->stop();
        emit signal_startTest(_i32AixsNo,0x12,0,0);//关使能
        _pStartBtn->setText("开始测试");
        _bStartTest = false;
        _pLogWidget->onShowlog("停止测试！",true);
    }
}
/**
* @brief
* 查看pid参数
*/
void QCurrentTestWidget::onQueryParamBtnClicked()
{
    emit signal_getpid(_i32AixsNo,0x31);
    emit signal_getpid(_i32AixsNo,0x32);
    emit signal_getpid(_i32AixsNo,0x33);
}
/**
* @brief
*
**/
void QCurrentTestWidget::onSetParamBtnClicked()
{
    float lfKP = _pKPEdit->text().toFloat();
    float lfKI = _pKIEdit->text().toFloat();
    float lfKD = _pKDEdit->text().toFloat();
    emit signal_setpid(_i32AixsNo,0x61,lfKP);//注入参数
    emit signal_setpid(_i32AixsNo,0x62,lfKI);//注入参数
    emit signal_setpid(_i32AixsNo,0x63,lfKD);//注入参数

}





/**
* @brief
*
**/
void QCurrentTestWidget::plotRcvData()
{
    if(_bStartTest == false)
    {
        return;
    }
    _pCurrentPlot->graph(0)->setData(_xdatalist,_ydatalist[0]);
    _pCurrentPlot->graph(1)->setData(_xdatalist,_ydatalist[1]);

    _pCurrentABPlot->graph(0)->setData(_xdatalist,_y1datalist[0]);
    _pCurrentABPlot->graph(1)->setData(_xdatalist,_y1datalist[1]);

    double lfxmax = *std::max_element(std::begin(_xdatalist), std::end(_xdatalist));
    double lfxmin = *std::min_element(std::begin(_xdatalist), std::end(_xdatalist));

    double lfymax0 = *std::max_element(std::begin(_ydatalist[1]), std::end(_ydatalist[1]));
    double lfymin0 = *std::min_element(std::begin(_ydatalist[1]), std::end(_ydatalist[1]));

    double lfy1max0 = *std::max_element(std::begin(_y1datalist[1]), std::end(_y1datalist[1]));
    double lfy1min0 = *std::min_element(std::begin(_y1datalist[1]), std::end(_y1datalist[1]));


    if(lfxmax > 5000)
    {
        lfxmin = lfxmax - 5000;
    }

    if(lfxmax < 5000)
    {
        lfxmax = 5000;
    }
    _pCurrentPlot->xAxis->setRange((lfxmin),(lfxmax));//幅度数据
    _pCurrentPlot->yAxis->setRange(lfymin0,lfymax0*1.1);//幅度数据
    _pCurrentPlot->replot();

    _pCurrentABPlot->xAxis->setRange((lfxmin),(lfxmax));//幅度数据
    _pCurrentABPlot->yAxis->setRange(lfy1min0,lfy1max0*1.1);//幅度数据
    _pCurrentABPlot->replot();
}
/**
* @brief
*
**/
void QCurrentTestWidget::setButtonStyleSheet(QPushButton* pBtn)
{
    pBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(245,90,70);;color:white;}"
                "QPushButton:unchecked { background-color: rgb(200,200,200);;color:black;}"
                "QPushButton{background-color: rgb(200,200,200);padding:5px;border-radius:5px;color:black;"
                "font-size:18px;font-family: 'Microsoft YaHei';}");


//    pBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    pBtn->setMinimumHeight(20);
}

/**
* @brief
*
**/
void QCurrentTestWidget::onRcvPidParam(QVariant var)
{
    st_pid_frame l_st_frame = var.value<st_pid_frame>();
    parsePIDParam(l_st_frame);

    QString sInfo;
    sInfo = sInfo + QString("当前KP:%1\n").arg(m_st_current_pid.lfKP);
    sInfo = sInfo + QString("当前KI:%1\n").arg(m_st_current_pid.lfKI);
    sInfo = sInfo + QString("当前KD:%1\n").arg(m_st_current_pid.lfKD);
    _pShowPIDLabel->setText(sInfo);
}

/**
* @brief
*
**/
void QCurrentTestWidget::rcvFrameData(st_loopbackdata rcvdata)
{
    if(_bStartTest == false)
    {
        return;
    }

    _i32FrameCnt++;

    parseParam(rcvdata);

    //显示状态
    QString sInfo = getFrameStatus(rcvdata);
    _pShowStatusLabel->setText(sInfo);

    //添加数据
    _xdatalist.append(_i32FrameCnt*M_FRAME_TIME);

    _ydatalist[0].append(_lfQSetdata);
    _ydatalist[1].append(_lfQResponsedata);

    _y1datalist[0].append(_lfACurrentdata);
    _y1datalist[1].append(_lfBCurrentdata);
}
